package org.dolphin.action.inventory;

import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.dolphin.base.bean.ProductCategoryVO;
import org.dolphin.base.constant.Status;
import org.dolphin.base.json.PaginationBean;
import org.dolphin.base.json.StatusBean;
import org.dolphin.entity.inventory.ProductCategory;
import org.dolphin.service.ProductService;
import org.dolphin.util.ExceptionHelper;
import org.dolphin.util.Pagination;
import org.hibernate.criterion.Order;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "custom-default")
@Namespace(value = "/inventory/productCategory")
@InterceptorRefs(value = {@InterceptorRef(value = "customStack")})
public class ProductCategoryAction extends ActionSupport {
  private static final long serialVersionUID = -5434590119192625396L;
  private ProductService productService;
  private Integer page;
  private Integer rows;
  private String sidx;
  private String sord;
  private PaginationBean pagination;
  private StatusBean status;
  private ProductCategoryVO productCategory;

  @Action(value = "addProductCategory", results = @Result(name = "success", location = "addProductCategory.jsp"))
  public String add() {
    return SUCCESS;
  }

  @Action(value = "createProductCategory", results = @Result(name = "success", params = {"root",
      "status"}, type = "json"))
  public String createProductCategory() {
    boolean error = false;
    String msg = "";
    status = new StatusBean();
    try {
      String name = productCategory.getName();
      String description = productCategory.getDescription();
      ProductCategory pc = new ProductCategory();
      pc.setName(name);
      pc.setDescription(description);
      getProductService().createProductCategory(pc);
    } catch (Exception e) {
      error = true;
      msg = ExceptionHelper.getRootCause(e).getMessage();
    } finally {
      status.setStatus(error ? Status.ERROR : Status.SUCCESS);
      if (error && msg != null) {
        status.addError(msg);
      }
    }
    return SUCCESS;
  }

  @Action(value = "editProductCategory", results = @Result(name = "success", location = "editProductCategory.jsp"))
  public String edit() {
    String id = productCategory.getId();
    productCategory = getProductService().getProductCategory(id);
    return SUCCESS;
  }

  public Integer getPage() {
    return page;
  }

  public PaginationBean getPagination() {
    return pagination;
  }

  @Action(value = "getProductCategories", results = @Result(name = "success", params = {"root",
      "pagination"}, type = "json"))
  public String getProductCategories() {
    List<Order> orders = null;
    if (StringUtils.hasText(getSidx())) {
      orders = new ArrayList<Order>(1);
      Order order = ObjectUtils.nullSafeEquals("desc", getSord()) ? Order.desc(getSidx()) : Order
          .asc(getSidx());
      orders.add(order);
    }
    List<ProductCategoryVO> results = getProductService().getProductCategories(page, rows, orders);
    Long records = getProductService().getProductCategoriesCount();
    Integer total = Pagination.calculateTotalPage(records, getRows());
    pagination = new PaginationBean();
    pagination.setPage(getPage());
    pagination.setRecords(records);
    pagination.setTotal(total);
    pagination.setRows(results);
    return SUCCESS;
  }

  public ProductCategoryVO getProductCategory() {
    return productCategory;
  }

  public ProductService getProductService() {
    return productService;
  }

  public Integer getRows() {
    return rows;
  }

  public String getSidx() {
    return sidx;
  }

  public String getSord() {
    return sord;
  }

  public StatusBean getStatus() {
    return status;
  }

  @Action(value = "listProductCategory", results = @Result(name = "success", location = "listProductCategory.jsp"))
  public String list() {
    return SUCCESS;
  }

  public void setPage(Integer page) {
    this.page = page;
  }

  public void setPagination(PaginationBean pagination) {
    this.pagination = pagination;
  }

  public void setProductCategory(ProductCategoryVO productCategory) {
    this.productCategory = productCategory;
  }

  public void setProductService(ProductService productService) {
    this.productService = productService;
  }

  public void setRows(Integer rows) {
    this.rows = rows;
  }

  public void setSidx(String sidx) {
    this.sidx = sidx;
  }

  public void setSord(String sord) {
    this.sord = sord;
  }

  public void setStatus(StatusBean status) {
    this.status = status;
  }

  @Action(value = "updateProductCategory", results = @Result(name = "success", params = {"root",
      "status"}, type = "json"))
  public String updateProductCategory() {
    boolean error = false;
    String msg = "";
    status = new StatusBean();
    ProductCategory pc = null;
    try {
      String id = productCategory.getId();
      pc = getProductService().fetchProductCategory(id);
      pc.setName(productCategory.getName());
      pc.setVersion(productCategory.getVersion());
      pc.setDescription(productCategory.getDescription());
      getProductService().updateProductCategory(pc);
    } catch (Exception e) {
      error = true;
      msg = ExceptionHelper.getRootCause(e).getMessage();
    } finally {
      status.setStatus(error ? Status.ERROR : Status.SUCCESS);
      if (error && msg != null) {
        status.addError(msg);
      } else {
        if (pc != null) {
          status.addAttr("version", pc.getVersion());
        }
      }
    }
    return SUCCESS;
  }

  @Action(value = "viewProductCategory", results = @Result(name = "success", location = "viewProductCategory.jsp"))
  public String view() {
    String id = productCategory.getId();
    productCategory = getProductService().getProductCategory(id);
    return SUCCESS;
  }
}
